﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;

using FrameworkLib.BackOffice;
using System.Xml;

namespace TestCrypto
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "<ROOT>AQID</ROOT>";
            bool result = false;
            try
            {
                XmlDocument myDoc = new XmlDocument();
                myDoc.LoadXml(str);

                result = true;
            }
            catch
            {
                result = false;
            }




            FrameworkLib.BackOffice.Test t = new FrameworkLib.BackOffice.Test();
            t.Nome = "giuseppe";
            t.Cognome = "dattilo";

            var xmlObj = t.Serializer().ToXml();


            FrameworkLib.BackOffice.Test3 t3 = new FrameworkLib.BackOffice.Test3();
            t3.Nome3 = "giuseppe3";
            t3.Cognome3 = "dattilo3";

            var xmlObj2 = t3.Serializer().ToXml();




            //TestCrypto.Configuration cfgS = TestCrypto.Configuration.GetConfiguration();
            //string ppp = cfgS.Message;

            //string messageFromConfiguration = TestCrypto.Configuration.GetConfiguration().Message;


            TestCrypto.MySection cfgS = TestCrypto.MySection.GetConfiguration();
            //System.Reflection.PropertyInfo ppp = cfgS.GetType().GetProperty("message");
            //string pppp = cfgS.Message;


            string hhh = (string)cfgS.GetPropertyValue("message");








            // sign test...
            byte[] signature = Security.Sign("Test", "CN=BetPoint");

            // Verify signature. Testcert.cer corresponds to "cn=my cert subject"
            if (Security.VerifyFromString("Test", signature, @"C:\Chiavi\prova2\PublicKey.pem"))
            {
                Console.WriteLine("Signature verified");
            }
            else
            {
                Console.WriteLine("ERROR: Signature not valid!");
            }














            //http://www.codeproject.com/Articles/10877/Public-Key-RSA-Encryption-in-C-NET
            Org.BouncyCastle.Crypto.AsymmetricKeyParameter obj = ReadAsymmetricKeyParameter(@"c:\chiavi\bet3publickey.pem");

            RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)obj;

            RSAParameters rsaParameters = new RSAParameters();
            rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned();
            rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned();
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(rsaParameters);
            
            //DotNetUtilities.ToRSAParameters(obj);

            //Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair;

            //using (var reader = File.OpenText(@"c:\chiavi\bet3publickey.pem"))
            //    keyPair = (Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair)new Org.BouncyCastle.OpenSsl.PemReader(reader).ReadObject();

            //RSAParameters key = ImportRSAKey(@"c:\chiavi\bet3publickey.pem");
        }

        public static Org.BouncyCastle.Crypto.AsymmetricKeyParameter ReadAsymmetricKeyParameter(string pemFilename)
        {
            var fileStream = System.IO.File.OpenText(pemFilename);
            var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(fileStream);
            var KeyParameter = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pemReader.ReadObject();
            return KeyParameter;
        }

        // Import ASymmetric RSA Key from a system file.
        public static RSAParameters ImportRSAKey(String fileName)
        {

            // Create a stream to a the specified system file.
            Stream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            // Extract/Deserialize the key from the file.
            IFormatter soapFormatter = new SoapFormatter();
            RSAParameters rsaParameter =
               (RSAParameters)soapFormatter.Deserialize(fileStream);

            // Close the file stream.
            fileStream.Close();

            return rsaParameter;

        }
    }
}
